namespace BEGDC.CC.Web.UserControls.ECS
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.Collections;

	using BEGDC.CC.BL.CCFlow.ECS;
	using BEGDC.CC.Entity;
	using BEGDC.CC.BL.Workflow;
	using BEGDC.CC.BL.Common;
	using BEGDC.CC.BL.Common.Query;
	using BEGDC.Utility.Castle;

	/// <summary>
	///		Summary description for CloseCtl.
	/// </summary>
	public class CloseCtl : BaseECS
	{
		protected System.Web.UI.HtmlControls.HtmlTableRow TR_EZVisitID;
		protected System.Web.UI.WebControls.TextBox tbSummary;
		protected System.Web.UI.WebControls.TextBox tbEZVisitID;
		protected System.Web.UI.WebControls.TextBox tbLegalID;
		protected System.Web.UI.WebControls.DataGrid dgCostCenter;
		protected System.Web.UI.HtmlControls.HtmlImage CostCenterPaneImg;
		private bool _isCoveredPerson = false;
		protected bool isReadOnly = true;

		public bool isCoveredPerson
		{
			get
			{ 
				return this._isCoveredPerson;
			}
			set
			{ 
				this._isCoveredPerson = value;
			}
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if(!Page.IsPostBack)
			{
				if(isReadOnly)
				{
					CostCenterPaneImg.Visible = false;
					this.tbEZVisitID.Enabled = false;
					this.tbLegalID.Enabled = false;
					this.tbSummary.Enabled = false;
					BindApplication();
					BindCostCenter();
					this.dgCostCenter.Columns[5].Visible = false;
				}
				if(!this.isCoveredPerson)
				{
					this.TR_EZVisitID.Visible = false;
				}
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.dgCostCenter.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgCostCenter_DeleteCommand);
			this.dgCostCenter.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgCostCenter_ItemDataBound);
		}
		#endregion

		private void dgCostCenter_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			try
			{
				if(Request.Form["cbDel"] == null || Request.Form["cbDel"] == "1")
				{
					return;
				}

				int rowid = Convert.ToInt32(e.Item.Cells[0].Text);
			
				DataTable contractInfo = (DataTable)Session[SessionFlag.CostCenter];
				DataRow contractRow = contractInfo.Rows[rowid-1];
				contractInfo.Rows.Remove(contractRow);
				this.BindCostCenter();
			}
			catch
			{	
			}
		}

		private void dgCostCenter_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
			{
				ImageButton Btn_Edit = (ImageButton) e.Item.Cells[3].FindControl("Btn_CostEdit");
				string ItemId = e.Item.Cells[0].Text.ToString();
				Btn_Edit.Attributes.Add("onclick", "OpenActualCostCenterPane('"+ItemId+"');");

				ImageButton ld = (ImageButton)e.Item.Cells[3].FindControl("Btn_CostDelete");
				if(ld != null)
				{
					ld.Attributes.Add("onclick","On_Submit();");
				}
			}
		}

		public void BindCostCenter()
		{
			DataTable CostCenter = null;
			if(Session[SessionFlag.CostCenter] != null)
			{
				CostCenter = (DataTable)Session[SessionFlag.CostCenter];
			}
			else
			{
				if(this.AppID > 0)
				{
					IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
					CostCenter = queryService.QueryCostCenterByAppId(this.AppID, CostType.Actual);
				}
				if(CostCenter == null || CostCenter.Rows.Count == 0)
				{
					CostCenter = new DataTable();
					CostCenter.Columns.Add(new DataColumn("Id", typeof(System.String)));
					CostCenter.Columns.Add(new DataColumn("CostCenterCode", typeof(System.String)));
					CostCenter.Columns.Add(new DataColumn("Cost", typeof(System.String)));
					CostCenter.Columns.Add(new DataColumn("ActualCost", typeof(System.String)));
					CostCenter.Columns.Add(new DataColumn("ActualTransferCost", typeof(System.String)));
				}
				Session[SessionFlag.CostCenter] = CostCenter;
			}
			this.dgCostCenter.DataSource = this.SetRowID(CostCenter);
			this.dgCostCenter.DataBind();
		}

		private DataTable SetRowID(DataTable table)
		{
			if(table != null && table.Rows.Count >0)
				for(int i=1; i<=table.Rows.Count; i++)
				{
					table.Rows[i-1][0] = i.ToString();
				}
			return table;
		}

		public void SaveApplication()
		{
			IECS ECSService = AppContainer.Instance[ServiceConst.ECSService] as IECS;
			BEGDC.CC.Entity.Application App = this.Application;
			App.EZVisitReqNo = this.tbEZVisitID.Text;
			App.LGDNo = this.tbLegalID.Text;
			App.Summary = this.tbSummary.Text;

			IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
			PaymentInfo payment = queryService.GetPaymentInfo(this.AppID);

			ArrayList cost = new ArrayList();
			DataTable table = (DataTable)this.dgCostCenter.DataSource;
			if(table != null && table.Rows.Count > 0)
			{
				for(int i=0;i<table.Rows.Count;i++)
				{
					CostCenterRelatedCost co = new CostCenterRelatedCost();
					co.CostCenterCode = table.Rows[i]["CostCenterCode"].ToString();
					co.DecryptedCost = Convert.ToDecimal(table.Rows[i]["Cost"]);
					if(table.Rows[i]["ActualCost"].ToString() == string.Empty)
						co.DecryptedActualCost = 0;
					else
						co.DecryptedActualCost = Convert.ToDecimal(table.Rows[i]["ActualCost"]);
					if(table.Rows[i]["ActualTransferCost"].ToString() == string.Empty)
						co.DecryptedActualTransferCost = 0;
					else
						co.DecryptedActualTransferCost = Convert.ToDecimal(table.Rows[i]["ActualTransferCost"]);
					co.CostType = CostType.Actual;
					co.PaymentInfo = payment;
					cost.Add(co);
				}
			}
			
			ECSService.SaveCloseApp(App, cost, payment.Id);
		}

		public void SubmitApplication()
		{
			IECS ECSService = AppContainer.Instance[ServiceConst.ECSService] as IECS;
			BEGDC.CC.Entity.Application App = this.Application;
			App.EZVisitReqNo = this.tbEZVisitID.Text;
			App.LGDNo = this.tbLegalID.Text;
			App.Summary = this.tbSummary.Text;

			IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
			PaymentInfo payment = queryService.GetPaymentInfo(this.AppID);

			ArrayList cost = new ArrayList();
			DataTable table = (DataTable)this.dgCostCenter.DataSource;
			if(table != null && table.Rows.Count > 0)
			{
				for(int i=0;i<table.Rows.Count;i++)
				{
					CostCenterRelatedCost co = new CostCenterRelatedCost();
					co.CostCenterCode = table.Rows[i]["CostCenterCode"].ToString();
					co.DecryptedCost = Convert.ToDecimal(table.Rows[i]["Cost"]);
					if(table.Rows[i]["ActualCost"].ToString() == string.Empty)
						co.DecryptedActualCost = 0;
					else
						co.DecryptedActualCost = Convert.ToDecimal(table.Rows[i]["ActualCost"]);
					if(table.Rows[i]["ActualTransferCost"].ToString() == string.Empty)
						co.DecryptedActualTransferCost = 0;
					else
						co.DecryptedActualTransferCost = Convert.ToDecimal(table.Rows[i]["ActualTransferCost"]);
					co.CostType = CostType.Actual;
					co.PaymentInfo = payment;
					cost.Add(co);
				}
			}
			
			ECSService.SubmitCloseApp(App, cost, payment.Id);
		}

		public void BindApplication()
		{
			this.LoadApplication();
			if(this.Application!=null)
			{
				this.tbEZVisitID.Text = this.Application.EZVisitReqNo;
				this.tbLegalID.Text = this.Application.LGDNo;
				this.tbSummary.Text = this.Application.Summary;
			}
		}

		public void SetEnableStatus()
		{
			isReadOnly = false;
			CostCenterPaneImg.Visible = true;
			this.tbEZVisitID.Enabled = true;
			this.tbLegalID.Enabled = true;
			this.tbSummary.Enabled = true;
		}

	}
}
